package com.copm.ifm.common.mq.rabbit.config;

import com.copm.ifm.common.mq.rabbit.RabbitConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.SerializerMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * rabbit配置信息类
 *
 * @author tp
 */
@Configuration
public class RabbitConfig {

    /**
     * spring的Bean默认都是单例
     * 设置为多例，防止在手动签收的时候报
     * Only one ConfirmCallback is supported by each RabbitTemplate异常
     */
    @Bean
    @Scope("prototype")
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMandatory(true);
        template.setMessageConverter(new SerializerMessageConverter());
        return template;
    }

    /**
     * 配置直连交换机
     */
    @Bean("ifm_handler_exchange")
    public DirectExchange ifmBootDirectExchange() {
        return new DirectExchange(RabbitConstants.Exchange.IFM_HANDLER_EXCHANGE, true, false);
    }

    /**
     * 异常处理队列
     */
    @Bean
    public Queue exceptionQueue() {
        return new Queue(RabbitConstants.Queue.EXCEPTION_HANDLER, true);
    }

    /**
     * -------------------------------------业务类型队列信息-----------------------------------------------
     * durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
     * exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
     * autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
     * 一般设置一下队列的持久化就好,其余两个就是默认false
     */
//    @Bean("ifm_handler")
//    public Queue ifmBootQueue() {
//        Map<String, Object> args = new HashMap<>(2);
//        args.put("ifm_dead_exchange", RabbitConstants.Exchange.IFM_DEADLETTER_EXCHANGE);
//        args.put("ifm_dead_key", RabbitConstants.Key.IFM_DEADLETTER_KEY);
//        return new Queue(RabbitConstants.Queue.IFM_HANDLER,true,false,false,args);
//    }
    @Bean("ifm_handler")
    public Queue ifmBootQueue() {
        return new Queue(RabbitConstants.Queue.IFM_HANDLER, true);
    }

    @Bean
    public Binding ifmBootBinder() {
        return BindingBuilder.bind(ifmBootQueue()).to(ifmBootDirectExchange()).with(RabbitConstants.Key.IFM_HANDLER);
    }

//    /**
//     * -------------------------------------消息中心的队列信息------------------------------------------
//     */
//    @Bean
//    public Queue immediateQueue() {
//        return new Queue(RabbitMqCommon.DELAYED_QUEUE_NAME);
//    }
//
//    @Bean
//    public CustomExchange customExchange() {
//        Map<String, Object> args = new HashMap<>();
//        args.put("x-delayed-type", "direct");
//        return new CustomExchange(RabbitMqCommon.DELAYED_EXCHANGE_NAME, "x-delayed-message", true, false, args);
//    }
//
//    @Bean
//    public Binding bindingNotify(@Qualifier("immediateQueue") Queue queue,
//                                 @Qualifier("customExchange") CustomExchange customExchange) {
//        return BindingBuilder.bind(queue).to(customExchange).with(RabbitMqCommon.DELAYED_ROUTING_KEY).noargs();
//    }


}
